package com.example.jf.jfapp.bean.base;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.example.jf.jfapp.R;
import com.example.jf.jfapp.bean.request.NearUserRequest;
import com.example.jf.jfapp.bean.response.NearUserResponse;
import com.example.jf.jfapp.utils.Constant;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import static com.bumptech.glide.gifdecoder.GifHeaderParser.TAG;

public  abstract  class BaseListActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_list);
        initData();
        RecyclerView rl = (RecyclerView) findViewById(R.id.rl);
        rl.setLayoutManager(new LinearLayoutManager(this));
        rl.setAdapter(getAdapter());
    }
    public abstract void initData();
    public abstract RecyclerView.Adapter getAdapter();

    public void  getList(double latitude,double longitude){
        OkHttpClient okHttpClient = new OkHttpClient();

        NearUserRequest nearUserRequest = new NearUserRequest(latitude,longitude);
        String json=new Gson().toJson(nearUserRequest);
        MediaType JSONTYPE = MediaType.parse("application/json;charset=utf-8");
        RequestBody body = RequestBody.create(JSONTYPE, json);
        //final Request request = RequestUtils.requestPOST(nearUserRequest, Constant.URL.GETNEARUSER);
        Request request=new Request.Builder()
                .addHeader("X-LC-Id", "S5KdTtqGDUwpImMvQ6HAJYaL-gzGzoHsz")
                .addHeader("X-LC-Key", "NRPgCGiGxjbtGN2Bn0mlglrL")
                .addHeader("Content-Type", "application/json")
                .url(Constant.URL.GETNEARUSER)
                .post(body)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String reuslt = response.body().string();
                Log.e(TAG, "onResponse: 附近的用户" + reuslt);
                NearUserResponse nearUserResponse = new Gson().fromJson(reuslt, NearUserResponse.class);

            }
        });



    }


}
